﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL.CarrefourCover
{
	/// <summary>
	/// 数据访问类:Baby
	/// </summary>
	public partial class Baby
	{
		public Baby()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "Baby"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Baby");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.CarrefourCover.Baby model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Baby(");
			strSql.Append("id,BabyName,BabyBirthday,BabySex,Weight,Height,Introduction,MilkBrand,BabyHeadImg,ParentFirstName,ParentLastName,ParentType,LiveProvince,LiveCity,LiveArea,LiveRoad,ZipCode,EMail,BabyTeamName,BabyTeamImg,BabyTeamDeclaration,ConfirmTeamId,ConfirmTeamName,Status,CreateTime,Ip)");
			strSql.Append(" values (");
			strSql.Append("@id,@BabyName,@BabyBirthday,@BabySex,@Weight,@Height,@Introduction,@MilkBrand,@BabyHeadImg,@ParentFirstName,@ParentLastName,@ParentType,@LiveProvince,@LiveCity,@LiveArea,@LiveRoad,@ZipCode,@EMail,@BabyTeamName,@BabyTeamImg,@BabyTeamDeclaration,@ConfirmTeamId,@ConfirmTeamName,@Status,@CreateTime,@Ip)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
                    new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@BabyName", SqlDbType.NVarChar,50),
					new SqlParameter("@BabyBirthday", SqlDbType.DateTime),
					new SqlParameter("@BabySex", SqlDbType.Int,4),
					new SqlParameter("@Weight", SqlDbType.Int,4),
					new SqlParameter("@Height", SqlDbType.Int,4),
					new SqlParameter("@Introduction", SqlDbType.NVarChar,500),
					new SqlParameter("@MilkBrand", SqlDbType.NVarChar,50),
					new SqlParameter("@BabyHeadImg", SqlDbType.NVarChar,200),
					new SqlParameter("@ParentFirstName", SqlDbType.NVarChar,50),
					new SqlParameter("@ParentLastName", SqlDbType.NVarChar,50),
					new SqlParameter("@ParentType", SqlDbType.Int,4),
					new SqlParameter("@LiveProvince", SqlDbType.NVarChar,50),
					new SqlParameter("@LiveCity", SqlDbType.NVarChar,50),
					new SqlParameter("@LiveArea", SqlDbType.NVarChar,50),
					new SqlParameter("@LiveRoad", SqlDbType.NVarChar,50),
					new SqlParameter("@ZipCode", SqlDbType.NVarChar,50),
					new SqlParameter("@EMail", SqlDbType.NVarChar,200),
					new SqlParameter("@BabyTeamName", SqlDbType.NVarChar,50),
					new SqlParameter("@BabyTeamImg", SqlDbType.NVarChar,200),
					new SqlParameter("@BabyTeamDeclaration", SqlDbType.NVarChar,200),
					new SqlParameter("@ConfirmTeamId", SqlDbType.Int,4),
					new SqlParameter("@ConfirmTeamName", SqlDbType.NVarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Ip", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Id;
			parameters[1].Value = model.BabyName;
			parameters[2].Value = model.BabyBirthday;
			parameters[3].Value = model.BabySex;
			parameters[4].Value = model.Weight;
			parameters[5].Value = model.Height;
			parameters[6].Value = model.Introduction;
			parameters[7].Value = model.MilkBrand;
			parameters[8].Value = model.BabyHeadImg;
			parameters[9].Value = model.ParentFirstName;
			parameters[10].Value = model.ParentLastName;
			parameters[11].Value = model.ParentType;
			parameters[12].Value = model.LiveProvince;
			parameters[13].Value = model.LiveCity;
			parameters[14].Value = model.LiveArea;
			parameters[15].Value = model.LiveRoad;
			parameters[16].Value = model.ZipCode;
			parameters[17].Value = model.EMail;
			parameters[18].Value = model.BabyTeamName;
			parameters[19].Value = model.BabyTeamImg;
			parameters[20].Value = model.BabyTeamDeclaration;
			parameters[21].Value = model.ConfirmTeamId;
			parameters[22].Value = model.ConfirmTeamName;
			parameters[23].Value = model.Status;
			parameters[24].Value = model.CreateTime;
			parameters[25].Value = model.Ip;

            return DbHelperSQL.ExecuteSql( strSql.ToString(), parameters );
            //object obj = DbHelperSQL.GetSingle( strSql.ToString(), parameters );
            //if (obj == null)
            //{
            //    return 0;
            //}
            //else
            //{
            //    return Convert.ToInt32( obj );
            //}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.CarrefourCover.Baby model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Baby set ");
			strSql.Append("BabyName=@BabyName,");
			strSql.Append("BabyBirthday=@BabyBirthday,");
			strSql.Append("BabySex=@BabySex,");
			strSql.Append("Weight=@Weight,");
			strSql.Append("Height=@Height,");
			strSql.Append("Introduction=@Introduction,");
			strSql.Append("MilkBrand=@MilkBrand,");
			strSql.Append("BabyHeadImg=@BabyHeadImg,");
			strSql.Append("ParentFirstName=@ParentFirstName,");
			strSql.Append("ParentLastName=@ParentLastName,");
			strSql.Append("ParentType=@ParentType,");
			strSql.Append("LiveProvince=@LiveProvince,");
			strSql.Append("LiveCity=@LiveCity,");
			strSql.Append("LiveArea=@LiveArea,");
			strSql.Append("LiveRoad=@LiveRoad,");
			strSql.Append("ZipCode=@ZipCode,");
			strSql.Append("EMail=@EMail,");
			strSql.Append("BabyTeamName=@BabyTeamName,");
			strSql.Append("BabyTeamImg=@BabyTeamImg,");
			strSql.Append("BabyTeamDeclaration=@BabyTeamDeclaration,");
			strSql.Append("ConfirmTeamId=@ConfirmTeamId,");
			strSql.Append("ConfirmTeamName=@ConfirmTeamName,");
			strSql.Append("Status=@Status,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("Ip=@Ip");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@BabyName", SqlDbType.NVarChar,50),
					new SqlParameter("@BabyBirthday", SqlDbType.DateTime),
					new SqlParameter("@BabySex", SqlDbType.Int,4),
					new SqlParameter("@Weight", SqlDbType.Int,4),
					new SqlParameter("@Height", SqlDbType.Int,4),
					new SqlParameter("@Introduction", SqlDbType.NVarChar,500),
					new SqlParameter("@MilkBrand", SqlDbType.NVarChar,50),
					new SqlParameter("@BabyHeadImg", SqlDbType.NVarChar,200),
					new SqlParameter("@ParentFirstName", SqlDbType.NVarChar,50),
					new SqlParameter("@ParentLastName", SqlDbType.NVarChar,50),
					new SqlParameter("@ParentType", SqlDbType.Int,4),
					new SqlParameter("@LiveProvince", SqlDbType.NVarChar,50),
					new SqlParameter("@LiveCity", SqlDbType.NVarChar,50),
					new SqlParameter("@LiveArea", SqlDbType.NVarChar,50),
					new SqlParameter("@LiveRoad", SqlDbType.NVarChar,50),
					new SqlParameter("@ZipCode", SqlDbType.NVarChar,50),
					new SqlParameter("@EMail", SqlDbType.NVarChar,200),
					new SqlParameter("@BabyTeamName", SqlDbType.NVarChar,50),
					new SqlParameter("@BabyTeamImg", SqlDbType.NVarChar,200),
					new SqlParameter("@BabyTeamDeclaration", SqlDbType.NVarChar,200),
					new SqlParameter("@ConfirmTeamId", SqlDbType.Int,4),
					new SqlParameter("@ConfirmTeamName", SqlDbType.NVarChar,50),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Ip", SqlDbType.NVarChar,50),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.BabyName;
			parameters[1].Value = model.BabyBirthday;
			parameters[2].Value = model.BabySex;
			parameters[3].Value = model.Weight;
			parameters[4].Value = model.Height;
			parameters[5].Value = model.Introduction;
			parameters[6].Value = model.MilkBrand;
			parameters[7].Value = model.BabyHeadImg;
			parameters[8].Value = model.ParentFirstName;
			parameters[9].Value = model.ParentLastName;
			parameters[10].Value = model.ParentType;
			parameters[11].Value = model.LiveProvince;
			parameters[12].Value = model.LiveCity;
			parameters[13].Value = model.LiveArea;
			parameters[14].Value = model.LiveRoad;
			parameters[15].Value = model.ZipCode;
			parameters[16].Value = model.EMail;
			parameters[17].Value = model.BabyTeamName;
			parameters[18].Value = model.BabyTeamImg;
			parameters[19].Value = model.BabyTeamDeclaration;
			parameters[20].Value = model.ConfirmTeamId;
			parameters[21].Value = model.ConfirmTeamName;
			parameters[22].Value = model.Status;
			parameters[23].Value = model.CreateTime;
			parameters[24].Value = model.Ip;
			parameters[25].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Baby ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Baby ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.CarrefourCover.Baby GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,BabyName,BabyBirthday,BabySex,Weight,Height,Introduction,MilkBrand,BabyHeadImg,ParentFirstName,ParentLastName,ParentType,LiveProvince,LiveCity,LiveArea,LiveRoad,ZipCode,EMail,BabyTeamName,BabyTeamImg,BabyTeamDeclaration,ConfirmTeamId,ConfirmTeamName,Status,CreateTime,Ip from Baby ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			Maticsoft.Model.CarrefourCover.Baby model=new Maticsoft.Model.CarrefourCover.Baby();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["BabyName"]!=null && ds.Tables[0].Rows[0]["BabyName"].ToString()!="")
				{
					model.BabyName=ds.Tables[0].Rows[0]["BabyName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BabyBirthday"]!=null && ds.Tables[0].Rows[0]["BabyBirthday"].ToString()!="")
				{
					model.BabyBirthday=DateTime.Parse(ds.Tables[0].Rows[0]["BabyBirthday"].ToString());
				}
				if(ds.Tables[0].Rows[0]["BabySex"]!=null && ds.Tables[0].Rows[0]["BabySex"].ToString()!="")
				{
					model.BabySex=int.Parse(ds.Tables[0].Rows[0]["BabySex"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Weight"]!=null && ds.Tables[0].Rows[0]["Weight"].ToString()!="")
				{
					model.Weight=int.Parse(ds.Tables[0].Rows[0]["Weight"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Height"]!=null && ds.Tables[0].Rows[0]["Height"].ToString()!="")
				{
					model.Height=int.Parse(ds.Tables[0].Rows[0]["Height"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Introduction"]!=null && ds.Tables[0].Rows[0]["Introduction"].ToString()!="")
				{
					model.Introduction=ds.Tables[0].Rows[0]["Introduction"].ToString();
				}
				if(ds.Tables[0].Rows[0]["MilkBrand"]!=null && ds.Tables[0].Rows[0]["MilkBrand"].ToString()!="")
				{
					model.MilkBrand=ds.Tables[0].Rows[0]["MilkBrand"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BabyHeadImg"]!=null && ds.Tables[0].Rows[0]["BabyHeadImg"].ToString()!="")
				{
					model.BabyHeadImg=ds.Tables[0].Rows[0]["BabyHeadImg"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ParentFirstName"]!=null && ds.Tables[0].Rows[0]["ParentFirstName"].ToString()!="")
				{
					model.ParentFirstName=ds.Tables[0].Rows[0]["ParentFirstName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ParentLastName"]!=null && ds.Tables[0].Rows[0]["ParentLastName"].ToString()!="")
				{
					model.ParentLastName=ds.Tables[0].Rows[0]["ParentLastName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ParentType"]!=null && ds.Tables[0].Rows[0]["ParentType"].ToString()!="")
				{
					model.ParentType=int.Parse(ds.Tables[0].Rows[0]["ParentType"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LiveProvince"]!=null && ds.Tables[0].Rows[0]["LiveProvince"].ToString()!="")
				{
					model.LiveProvince=ds.Tables[0].Rows[0]["LiveProvince"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LiveCity"]!=null && ds.Tables[0].Rows[0]["LiveCity"].ToString()!="")
				{
					model.LiveCity=ds.Tables[0].Rows[0]["LiveCity"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LiveArea"]!=null && ds.Tables[0].Rows[0]["LiveArea"].ToString()!="")
				{
					model.LiveArea=ds.Tables[0].Rows[0]["LiveArea"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LiveRoad"]!=null && ds.Tables[0].Rows[0]["LiveRoad"].ToString()!="")
				{
					model.LiveRoad=ds.Tables[0].Rows[0]["LiveRoad"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ZipCode"]!=null && ds.Tables[0].Rows[0]["ZipCode"].ToString()!="")
				{
					model.ZipCode=ds.Tables[0].Rows[0]["ZipCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["EMail"]!=null && ds.Tables[0].Rows[0]["EMail"].ToString()!="")
				{
					model.EMail=ds.Tables[0].Rows[0]["EMail"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BabyTeamName"]!=null && ds.Tables[0].Rows[0]["BabyTeamName"].ToString()!="")
				{
					model.BabyTeamName=ds.Tables[0].Rows[0]["BabyTeamName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BabyTeamImg"]!=null && ds.Tables[0].Rows[0]["BabyTeamImg"].ToString()!="")
				{
					model.BabyTeamImg=ds.Tables[0].Rows[0]["BabyTeamImg"].ToString();
				}
				if(ds.Tables[0].Rows[0]["BabyTeamDeclaration"]!=null && ds.Tables[0].Rows[0]["BabyTeamDeclaration"].ToString()!="")
				{
					model.BabyTeamDeclaration=ds.Tables[0].Rows[0]["BabyTeamDeclaration"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ConfirmTeamId"]!=null && ds.Tables[0].Rows[0]["ConfirmTeamId"].ToString()!="")
				{
					model.ConfirmTeamId=int.Parse(ds.Tables[0].Rows[0]["ConfirmTeamId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ConfirmTeamName"]!=null && ds.Tables[0].Rows[0]["ConfirmTeamName"].ToString()!="")
				{
					model.ConfirmTeamName=ds.Tables[0].Rows[0]["ConfirmTeamName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Status"]!=null && ds.Tables[0].Rows[0]["Status"].ToString()!="")
				{
					model.Status=int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Ip"]!=null && ds.Tables[0].Rows[0]["Ip"].ToString()!="")
				{
					model.Ip=ds.Tables[0].Rows[0]["Ip"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,BabyName,BabyBirthday,BabySex,Weight,Height,Introduction,MilkBrand,BabyHeadImg,ParentFirstName,ParentLastName,ParentType,LiveProvince,LiveCity,LiveArea,LiveRoad,ZipCode,EMail,BabyTeamName,BabyTeamImg,BabyTeamDeclaration,ConfirmTeamId,ConfirmTeamName,Status,CreateTime,Ip ");
			strSql.Append(" FROM Baby ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,BabyName,BabyBirthday,BabySex,Weight,Height,Introduction,MilkBrand,BabyHeadImg,ParentFirstName,ParentLastName,ParentType,LiveProvince,LiveCity,LiveArea,LiveRoad,ZipCode,EMail,BabyTeamName,BabyTeamImg,BabyTeamDeclaration,ConfirmTeamId,ConfirmTeamName,Status,CreateTime,Ip ");
			strSql.Append(" FROM Baby ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Baby ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from Baby T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="orderby"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string orderby, out int totalCount)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@strGetFields", SqlDbType.VarChar, 255),
					new SqlParameter("@strOrder", SqlDbType.VarChar,255),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@recordCount", SqlDbType.Int),
					new SqlParameter("@doCount", SqlDbType.Bit),
					};
            parameters[0].Value = "Baby";
            parameters[1].Value = "*";
            parameters[2].Value = orderby;
            parameters[3].Value = strWhere;
            parameters[4].Value = PageIndex;
            parameters[5].Value = PageSize;
            parameters[6].Direction = ParameterDirection.Output;
            parameters[7].Value = 1;

            DataSet retSet = DbHelperSQL.RunProcedure("P_Common_Pager", parameters, "ds");
            totalCount = Convert.ToInt32(parameters[6].Value);
            return retSet;
        }


		#endregion  Method
	}
}

